package com.example.sea.back.web.user;

import com.alibaba.druid.util.StringUtils;
import com.example.sea.core.data.user.AuthRoleEnum;
import com.example.sea.core.data.user.entity.AuthPermission;
import com.example.sea.core.data.user.entity.AuthRole;
import com.example.sea.core.data.user.model.AuthPermissionModel;
import com.example.sea.core.data.user.service.AuthRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author zengyz
 * @date 2018/7/9 下午 3:09
 */
@Controller
@RequestMapping("/user/role")
public class UserRoleController {

    @Autowired
    private AuthRoleService authRoleService;


    /**
     * List页面
     * @param request
     * @param pageNum
     * @param pageSize
     * @param state
     * @param name
     * @return
     */
    @GetMapping("/index")
    public String indexHtml(HttpServletRequest request, Integer pageNum, Integer pageSize, Integer state, String name){
        Map params=new HashMap();
        params.put("pageNum",pageNum==null?1:pageNum);
        params.put("pageSize",pageSize==null?10:pageSize);
        params.put("state",state);
        params.put("name",StringUtils.isEmpty(name)?null:"%"+name+"%");
        params.put("type",AuthRoleEnum.TYPE_BACK_PERMISSION.getValue());
        PageInfo<AuthRole> pageInfo = authRoleService.findPageByParams(params);
        request.setAttribute("pageInfo",pageInfo);
        /**
         * 回显数据
         */
        request.setAttribute("state",state);
        request.setAttribute("name",name);
        return "user/role/index";
    }

    /**
     * 添加页面
     * @return
     */
    @GetMapping("/add")
    public String addHtml(){
        return "user/role/add";
    }


    /**
     * 添加
     * @param authRole
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public Map addSubmit(AuthRole authRole){
        Map resultMap=new HashMap();
        int add = authRoleService.add(authRole);
        if (add==1){
            resultMap.put("code",200);
        }else{
            resultMap.put("code",500);
        }
        return resultMap;
    }

    /**
     * 删除
     * @param request
     * @param chkids
     * @return
     */
    @ResponseBody
    @PostMapping("/delete")
    public Map deleteSubmit(HttpServletRequest request,String chkids){
        Map resultMap=new HashMap();
        String[] ids = chkids.split(",");
        int delete = authRoleService.delete(ids);
        if (delete!=ids.length){
            resultMap.put("code",500);
        }else{
            resultMap.put("code",200);
        }
        return resultMap;
    }

    /**
     * 修改状态
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/state")
    public Map stateSubmit(HttpServletRequest request,String id){
        Map resultMap=new HashMap();
        AuthRole ar = authRoleService.findById(id);
        if (ar.getState().equals(AuthRoleEnum.STATE_NO.getValue())){
            ar.setState(AuthRoleEnum.STATE_OK.getValue());
        }else if(ar.getState().equals(AuthRoleEnum.STATE_OK.getValue())){
            ar.setState(AuthRoleEnum.STATE_NO.getValue());
        }
        int update = authRoleService.update(ar);
        if (update==0){
            resultMap.put("code",500);
        }else{
            resultMap.put("code",200);
        }
        return resultMap;
    }

    /**
     * 跳转修改
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/update")
    public String updateHtml(HttpServletRequest request,String id){
        AuthRole authRole = authRoleService.findById(id);
        request.setAttribute("authRole",authRole);
        return "user/role/edit";
    }

    /**
     * 修改
     * @param authRole
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public Map updateSubmit(AuthRole authRole){

        AuthRole oldEntity = authRoleService.findById(authRole.getId());
        oldEntity.setCode(authRole.getCode());
        oldEntity.setName(authRole.getName());
        oldEntity.setRemark(authRole.getRemark());
        oldEntity.setState(authRole.getState());
        Map resultMap=new HashMap();
        int add = authRoleService.update(oldEntity);
        if (add==1){
            resultMap.put("code",200);
        }else{
            resultMap.put("code",500);
        }
        return resultMap;
    }

    /**
     * 配置权限页面
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/permission/index")
    public String rolePermissionIndexHtml(HttpServletRequest request,String id){
        AuthRole authRole = authRoleService.findById(id);
        request.setAttribute("authRole",authRole);
        List<AuthPermissionModel> authPermissionModels = authRoleService.findByIdToRolePermission(id);
        request.setAttribute("authPermissionModels",authPermissionModels);
        return "user/role/role-permission";
    }

    /**
     * 配置权限
     * @param request
     * @param chkids
     * @param roleId
     * @return
     */
    @ResponseBody
    @PostMapping("/permission/index")
    public Map rolePermissionIndexSubmit(HttpServletRequest request,String chkids,String roleId){
        authRoleService.updateRolePermission(chkids.split(","),roleId);
        Map resultMap=new HashMap();
        resultMap.put("code",200);
        return resultMap;
    }
}
